package cn.edu.neu.vmmigeval;

import cn.edu.neu.vmmigeval.cloud.entity.*;
import cn.edu.neu.vmmigeval.cloud.config.CloudletRegistry;
import cn.edu.neu.vmmigeval.cloud.config.HostRegistry;
import cn.edu.neu.vmmigeval.cloud.config.VmRegistry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yfwz100 on 16/2/25.
 */
public class CaseBuilder {

    public int initCloudlets(DataCenter datacenter, DataCenterBroker broker, CloudletRegistry clr) {
        String scale = datacenter.getScale();
        Map<CloudletRegistry, Integer> cloudletMaps = new HashMap<CloudletRegistry, Integer>();

        // 规模为2100的时候执行这个
        if (scale.equals("big")) {
            cloudletMaps.put(clr, 100);
        }
        //规模为1200的时候执行这个
        if (scale.equals("middle")) {
            cloudletMaps.put(clr, 1000);
        }
        //规模为600的时候执行这个
        if (scale.equals("small")) {
            cloudletMaps.put(clr, 600);//600
        }

        InitDatacenter idc = new InitDatacenter();

        return idc.initDatacenterBrokerCloudlets(broker, cloudletMaps, 0);
    }


    //init(DataCenter datacenter,DataCenterBroker broker,String guimo,String workloadType, CloudletRegistry clr)
    public DataCenter initHosts(String guimo, String workloadType) {
        /**
         * 对以下的类进行改写就能体现出具体的资源使用情况
         */
        PE pe1 = new PE(1000);
        PE pe2 = new PE(2000);
        PE pe3 = new PE(3000);

        List<PE> pes1 = new ArrayList<PE>();
        pes1.add(pe1);
        pes1.add(pe1);
        pes1.add(pe1);
        pes1.add(pe1);

        List<PE> pes2 = new ArrayList<PE>();
        pes2.add(pe2);
        pes2.add(pe2);
        pes2.add(pe2);
        pes2.add(pe2);

        List<PE> pes3 = new ArrayList<PE>();
        pes3.add(pe3);
        pes3.add(pe3);
        pes3.add(pe3);
        pes3.add(pe3);


        HostRegistry hr1 = new HostRegistry(pes1, pes1.size() * pe1.getMips(), 4000, 1000, 1000);
        HostRegistry hr2 = new HostRegistry(pes2, pes2.size() * pe2.getMips(), 8000, 1000, 1000);
        HostRegistry hr3 = new HostRegistry(pes3, pes3.size() * pe3.getMips(), 32000, 1000, 1000);

        HostRegistry hr4 = new HostRegistry(pes1, pes1.size() * pe1.getMips(), 4000, 1000, 1000);
        HostRegistry hr5 = new HostRegistry(pes2, pes2.size() * pe2.getMips(), 8000, 1000, 1000);
        HostRegistry hr6 = new HostRegistry(pes3, pes3.size() * pe3.getMips(), 32000, 1000, 1000);

        Map<HostRegistry, Integer> hostsMaps = new HashMap<HostRegistry, Integer>();


        //当规模是3*700 = 2100 台主机的时候  采用以下初始化方式
        if (guimo.equals("big")) {
            hostsMaps.put(hr1, 700);
            hostsMaps.put(hr2, 700);
            hostsMaps.put(hr3, 700);

//			 hostsMaps.put(hr4, 40);
//			 hostsMaps.put(hr5, 40);
//			 hostsMaps.put(hr6, 40);
        }


        //当规模是3*400 = 1200 台主机的时候  采用以下初始化方式
        if (guimo.equals("middle")) {
            hostsMaps.put(hr1, 400);
            hostsMaps.put(hr2, 400);
            hostsMaps.put(hr3, 400);

            hostsMaps.put(hr4, 30);
            hostsMaps.put(hr5, 30);
            hostsMaps.put(hr6, 30);
        }

        //当规模是3*200 = 600 台主机的时候  采用以下初始化方式
        if (guimo.equals("small")) {
            hostsMaps.put(hr1, 200);
            hostsMaps.put(hr2, 200);
            hostsMaps.put(hr3, 200);

            hostsMaps.put(hr4, 20);
            hostsMaps.put(hr5, 20);
            hostsMaps.put(hr6, 20);


        }
        InitDatacenter idc = new InitDatacenter();
        DataCenter dc = idc.initDatacenter(hostsMaps);
        dc.setScale(guimo);
        dc.setWorkloadType(workloadType);

        return dc;
    }


    public DataCenterBroker initVms(DataCenter datacenter) {
        // 对以下的类进行改写就能体现出具体的资源使用情况
        VmRegistry vr1 = new VmRegistry(1000, 500, 150, 150);
        VmRegistry vr2 = new VmRegistry(1000, 1000, 150, 150);
        VmRegistry vr3 = new VmRegistry(1000, 2000, 150, 150);
        VmRegistry vr4 = new VmRegistry(2000, 1000, 150, 150);
        VmRegistry vr5 = new VmRegistry(2000, 2000, 150, 150);
        VmRegistry vr6 = new VmRegistry(2500, 4000, 150, 150);

        String scale = datacenter.getScale();
        //String workloadType = datacenter.getWorkloadType();

        Map<VmRegistry, Integer> vmsMaps = new HashMap<VmRegistry, Integer>();

        //当规模是3*700 = 2100 台主机的时候  采用以下初始化方式
        if (scale.equals("big")) {
            vmsMaps.put(vr1, 1200);
            vmsMaps.put(vr2, 1200);
            vmsMaps.put(vr3, 1200);
            vmsMaps.put(vr4, 1200);
            vmsMaps.put(vr5, 1200);
            vmsMaps.put(vr6, 1200);

        }

        //当规模是3*400 = 1200 台主机的时候  采用以下初始化方式
        if (scale.equals("middle")) {
            vmsMaps.put(vr1, 700);
            vmsMaps.put(vr2, 700);
            vmsMaps.put(vr3, 700);
            vmsMaps.put(vr4, 700);
            vmsMaps.put(vr5, 700);
            vmsMaps.put(vr6, 700);
        }

        //当规模是3*200 = 600 台主机的时候  采用以下初始化方式
        if (scale.equals("small")) {
            vmsMaps.put(vr1, 350);
            vmsMaps.put(vr2, 350);
            vmsMaps.put(vr3, 350);
            vmsMaps.put(vr4, 350);
            vmsMaps.put(vr5, 350);
            vmsMaps.put(vr6, 350);
        }
        InitDatacenter idc = new InitDatacenter();
        return idc.initDatacenterBrokerVms(datacenter, vmsMaps, scale);
    }


    public CloudletRegistry getCloudletRegistry(DataCenter dc) {
        CloudletRegistry clr = null;
        String workloadType = dc.getWorkloadType();
        //cpu intensive

        UtilizationModel utilizationModel1 = new RandomUtilizationModel(0.0, 0.3);
        UtilizationModel utilizationModel2 = new RandomUtilizationModel(0.5, 1.0);

        if (workloadType.equals("cpu")) {
            clr = new CloudletRegistry(10000, utilizationModel2, utilizationModel1, utilizationModel1, utilizationModel1);
        }
        //ram intensive
        if (workloadType.equals("ram")) {
            clr = new CloudletRegistry(10000, new RandomUtilizationModel(0.0, 0.3),
                    new RandomUtilizationModel(0.7, 1.0), new RandomUtilizationModel(0.0, 0.3), new RandomUtilizationModel(0.0, 0.3));
        }
        //bw intensive
        if (workloadType.equals("bw")) {
            clr = new CloudletRegistry(10000, new RandomUtilizationModel(0.0, 0.3),
                    new RandomUtilizationModel(0.0, 0.3), new RandomUtilizationModel(0.7, 1.0), new RandomUtilizationModel(0.0, 0.3));
        }
        //disk intensive
        if (workloadType.equals("disk")) {
            clr = new CloudletRegistry(10000, new RandomUtilizationModel(0.0, 0.3),
                    new RandomUtilizationModel(0.0, 0.3), new RandomUtilizationModel(0.0, 0.3), new RandomUtilizationModel(0.7, 1.0));

        }
        return clr;

    }
}
